package a66weding.com.jiehuntong.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.HashMap;
import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.application.DataWareHourse;
import a66weding.com.jiehuntong.base.BaseActivity;
import a66weding.com.jiehuntong.beans.BudgetBean;
import a66weding.com.jiehuntong.beans.CommonResultBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.beans.User;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;
import a66weding.com.jiehuntong.pager.BasePager;
import a66weding.com.jiehuntong.pager.ToolsPager_Budget_Home;
import a66weding.com.jiehuntong.pager.ToolsPager_Budget_RecommendList;
import a66weding.com.jiehuntong.pager.ToolsPager_Budget_RecommendListEdit;

/**
 * Created by Administrator on 2016/6/19.
 */
public class ToolsWedBudgetActivity extends BaseActivity implements OnNetResultListener {
    private View mContentview;

    private HomeBaseFragment mFragment;

    private BudgetBean mBudgitData;
    private int budgetCount;
    private int index;

    private int tableCount;


    private static HashMap<String,BasePager> mBudgetPages;
    private View progressView;
    private int mCurrentPager;

    public static  int mPlaceResultCode=5;
    private static String cityName="北京";

    @Override
    protected void onPreInitViewExecute() {
        super.onPreInitViewExecute();
        mBudgetPages = new HashMap<String,BasePager>();
        mBudgetPages.put("结婚预算主页",new ToolsPager_Budget_Home(this));
        mBudgetPages.put("结婚预算明细页",new ToolsPager_Budget_RecommendList(this));
        mBudgetPages.put("编辑结婚预算明细页",new ToolsPager_Budget_RecommendListEdit(this));

        User user = DataWareHourse.getInstance().getmUser();

        index= getIntent().getIntExtra("wode",0);

           String url= UrlDealUtil.getRealUrl(RequestCode.RETURN_WBITEM);

           HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url,
                   this, BudgetBean.class, RequestCode.RETURN_WBITEM);
           HttpHandleUtil.executeCommonPost(this,httpRequestBean);



    }


    @Override
    protected View getContentView() {

        return null;

    }



    @Override
    protected void onPostCreateExecute() {
        initData();
    }


    private void initData() {

    }


    /**
     * 设置 Fragment 到布局中
     */
    public  void setFragment(String pagerName) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
            @Override
            public void onBackStackChanged() {
                
            }
        });
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        mFragment=new HomeBaseFragment();
        Bundle mBundle= new Bundle();
        mBundle.putString("pagerName",pagerName);

        mFragment.setArguments(mBundle);
        if(!"结婚预算明细页".equals(pagerName)) {
            transaction.addToBackStack(pagerName);
        }

        transaction.replace(R.id.flay_baseactivity_content, mFragment);

        transaction.commit();

    }




    public static  class HomeBaseFragment extends Fragment {

        public  HomeBaseFragment(){}
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                                 @Nullable Bundle savedInstanceState) {
            String pagerName = (String) getArguments().get("pagerName");
            BasePager pager = getBasePager(pagerName);
            if (null != pager)

                return pager.mRootView;
            return null;
        }

        @Override
        public void onDestroy() {

            super.onDestroy();
        }
    }


    public static  BasePager getBasePager(String pagerName) {
        BasePager pager =  mBudgetPages.get(pagerName);

        if (null != pager && !pager.mInit) {
            pager.mInit = true;
            pager.initData();
        }
        pager.notifyData();
        return pager;
    }

    @Override
    public void onSucess(SuccessBean sucessbean)  {
        Object data = sucessbean.getData();
        if(data instanceof BudgetBean) {
            BudgetBean butgitBean = (BudgetBean) data;
            switch (butgitBean.getResCode()) {
                    case "0000":
                        this.mBudgitData=butgitBean;
                        List<BudgetBean.DataBean> dataList = butgitBean.getData();
                        budgetCount=0;
                        for (int i = 0; i < dataList.size(); i++) {
                            budgetCount += dataList.get(i).getWBItemAmount();
                        }
                        setFragment("结婚预算明细页");
                    break;
                     case "0001":


                     case "0002":


                     case "0003":
                         setFragment("结婚预算主页");
                    break;
            }

        }else if(data instanceof CommonResultBean){

        }
    }

    @Override
    public void onError(ErrorBean errorBean) {

    }

    /**
     * 再按一次退出
     */
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            List<Fragment> fragments = getSupportFragmentManager().getFragments();
            if(fragments.size()==0) {
                finish();
            }
        }
        return super.onKeyUp(keyCode, event);
    }

    public void notifyPagerData(String pagerName){
        BasePager pager =  mBudgetPages.get(pagerName);
        pager.notifyData();

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==mPlaceResultCode) {
            if(data!=null) {
                String city = data.getStringExtra("city");
                setCityName(city);
                this.cityName=city;
                setFragment("结婚预算主页");
            }
        }
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getCityName() {
        return cityName;
    }

    public int getmPlaceResultCode() {
        return mPlaceResultCode;
    }

    public void setmPlaceResultCode(int mPlaceResultCode) {
        this.mPlaceResultCode = mPlaceResultCode;
    }

    public int getTableCount() {
        return tableCount;
    }

    public void setTableCount(int tableCount) {
        this.tableCount = tableCount;
    }

    public int getBudgetCount() {
        return budgetCount;
    }

    public void setBudgetCount(int budgetCount) {
        this.budgetCount = budgetCount;
    }

    public BudgetBean getmBudgitData() {
        return mBudgitData;
    }

    public void setmBudgitData(BudgetBean mBudgitData) {
        this.mBudgitData = mBudgitData;
    }


}
